	<div class="page-title">
		<div class="title_left">
			<h3>Rules</h3>
		</div>
	</div>
			<!-- Top Info Bar End -->
      <!-- Message -->
      <?php if(!empty($message)){ ?>
      <div class="row">
        <div class="col-md-3 col-sm-3 col-xs-12">
          <div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <?= $message ?>
          </div>
        </div>
      </div>
      <?php } ?>
      <!-- Message End -->
			<!-- Content -->				   
			<div class="row">			
			  <!-- Rules Log -->
			  <div class="col-md-8 col-sm-8 col-xs-12">
				 <div class="x_panel">
				  <div class="x_title">
					<h2>Logs <small></small></h2>
					<ul class="nav navbar-right panel_toolbox">
					  <li><a class="close-link"><i class="fa fa-close"></i></a>
					  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
					  </li>
					  </li>
					</ul>
					<div class="clearfix"></div>
				  </div>
				  <div class="x_content">
					<div class="panel panel-default">
						<div class="panel-body" style="max-height: 207px;overflow-y: scroll;">
						<?= nl2br($content['log']) ?>
						</div>
					</div>
				  </div>
				</div>
				<!-- End Log -->
				<!-- Rules List -->
				<div class="clearfix"></div>
				<div class="x_panel">
				  <div class="x_title">
					<h2>Rules <small></small></h2>
					<ul class="nav navbar-right panel_toolbox">
					  <li><a class="close-link"><i class="fa fa-close"></i></a>
					  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
					  </li>
					  </li>
					</ul>
					<div class="clearfix"></div>
				  </div>
				  <div class="x_content">
					<table id="rules" class="table table-striped table-bordered table-valign">
					  <thead>
						<tr>
						  <th>ID</th>
						  <th>Created</th>
						  <th>Counter</th>
						  <th>Action</th>
						  <th>Trigger</th>				  
						  <th>Global Trigger</th>
              <th>Whitelisted</th>
						  <th style="width:80px;">Manage</th>
						</tr>
					  </thead>
					  <tbody>
						<?php $i = 0;						
							foreach($content['rules'] as $rule){ 
						?>
								<tr>
									<td> <?= $rule->id ?></td>
									<td> <?= $rule->date ?></td>   
									<td> <?= $rule->uses ?></td>
									<td> <?php echo $rule->action . (($rule->action == "ban") ? " (".$rule->bantime." s)" : "") ?></td>
									<td> <?php echo (($rule->trigger != "client") ? $rule->trigger." > ".$rule->threshold : $rule->clientStr." Clients") ?></td>
									<td> <?php if($rule->global) echo (($rule->gTrigger == "traffic") ? "Traffic (MB)" : "Peer count")." > ".$rule->gThreshold ?></td>
                  <td> <?= ($rule->whitelisted) ? "true" : "false" ?></td>
									<td>
										<a class="btn btn-default btn-xs" href="?p=rules&c=edit&id=<?= $rule->id ?>&t=<?= $_SESSION["csfrToken"] ?>"> Edit</a>
										<a class="btn btn-default btn-xs" href="?p=rules&c=delete&id=<?= $rule->id ?>&t=<?= $_SESSION["csfrToken"] ?>"> Delete</a>
									</td>										   
								</tr>
						<?php } ?>						
					  </tbody>
					</table>
				  </div>
				</div>
			  </div>
			<!-- End Rules List -->		   
			
			<!-- Add Rule -->
			<div class="col-md-4 col-sm-4 col-xs-12">
			  <div>
				<div class="x_panel">
				  <div class="x_title">
					<h2><?php echo ((!isset($content['editRule']->id)) ? "Create New" : "Edit") ?> Rule </h2>
					<ul class="nav navbar-right panel_toolbox">
					  <li><a class="close-link"><i class="fa fa-close"></i></a>
					  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
					  <li><a><i class="fa fa-question-circle-o" data-toggle="tooltip" data-html="true" data-placement="left" title="
In the first row you choose what to do with rule violating peers (ban (in seconds) or disconnect the peer or just mention the violation in the logfile).<br><br>
In the second row you specify what the rule violation is (e.g. the peer caused too much traffic or the peer uses a specific client software).<br><br>
In the third row you can choose if you want that a (to the peer unrelated) trigger event needs to happen before the rule is applied (optional).<br><br>
In the same row you can also choose if the rule should be applied to your whitelisted peers (not recommended).
In the last fourth row you need to speficy the global trigger event if you checked the box above (e.g. all peers caused more traffic than the given treshold)."></i></a>
					  </li>
					</ul>
					<div class="clearfix"></div>
				  </div>
				<div class="x_content">
					<form class="form-inline" action="?p=rules&c=save&t=<?= $_SESSION["csfrToken"] ?>" method="POST">
						<input type="hidden" name="id" value="<?= $content['editRule']->id ?>" /> 
						
						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">
							<select class="form-control" name="action" id="action" style="width:150px;" onchange="displayInput(this.value, 'ban', 'bantime');">
								<option <?php echo (($content['editRule']->action == "ban") ? "selected" : "") ?> value="ban">Ban</option>
								<option <?php echo (($content['editRule']->action == "disconnect") ? "selected" : "") ?> value="disconnect">Disconnect</option>
								<option <?php echo (($content['editRule']->action == "notice") ? "selected" : "") ?> value="notice">Notice</option>
							</select> 
						</div>
						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">						   
							<input type="text" class="form-control" id="bantime" name="bantime" value="<?= $content['editRule']->bantime ?>" placeholder="Ban time (s)" maxlength="9" size="9">
						</div>
						
						<div class="clearfix"></div>
						
						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">
							<select class="form-control" name="trigger" id="trigger" onchange="displayInput2(this.value, 'client', 'clientselectiond', 'threshold');">
								<option value="traffic" <?php echo (($content['editRule']->trigger == "traffic") ? "selected" : "") ?>>Traffic (MB)</option>
								<option value="trafficin" <?php echo (($content['editRule']->trigger == "trafficin") ? "selected" : "") ?>>Traffic In (MB)</option>
								<option value="trafficout" <?php echo (($content['editRule']->trigger == "trafficout") ? "selected" : "") ?>>Traffic Out (MB)</option>
								<option value="client" <?php echo (($content['editRule']->trigger == "client") ? "selected" : "") ?>>Client</option>
								<!-- <option value="hosted" <?php //echo (($content['editRule']->trigger == "hosted") ? "selected" : "") ?>>Hosted (Server)</option> -->
								<option value="ping" <?php echo (($content['editRule']->trigger == "ping") ? "selected" : "") ?>>Ping (ms)</option>									  
							</select>	 
						</div>
						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">						
							<input type="text" class="form-control" id="threshold"  name="threshold" <?= $content['editRule']->threshold ?> value="<?= $content['editRule']->threshold ?>" placeholder="Threshold" maxlength="9" size="9">
							<div id="clientselectiond" name="clientselectiond" style="display: none;">
								<select id="clientselection" name="clientselection[]" multiple="multiple">
									<option <?php echo ((!empty($content['editRule']->clientArr) && in_array("ABC",$content['editRule']->clientArr)) ? "selected" : "") ?> value="ABC">ABC (Bcash)</option>
									<option <?php echo ((!empty($content['editRule']->clientArr) && in_array("Unlimited",$content['editRule']->clientArr)) ? "selected" : "") ?> value="Unlimited">Unlimited</option>	 
									<option <?php echo ((!empty($content['editRule']->clientArr) && in_array("bcoin",$content['editRule']->clientArr)) ? "selected" : "") ?> value="bcoin">bcoin</option>
									<option <?php echo ((!empty($content['editRule']->clientArr) && in_array("SPV",$content['editRule']->clientArr)) ? "selected" : "") ?> value="SPV">SPV (BRD, bitcoinj)</option>
								</select> 
							</div>
						</div>

						<div class="clearfix"></div>

						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">
							<label>
								<input type="checkbox" name="global" id="global" class="js-switch" <?php echo (($content['editRule']->global) ? "checked" : "") ?>/> Global Trigger
							</label>
						</div>

            <div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">
							<label>
								<input type="checkbox" name="whitelisted" id="whitelisted" class="js-switch" <?php echo (($content['editRule']->whitelisted) ? "checked" : "") ?>/> Include Whitelisted
							</label>
						</div>
						
						<div class="clearfix"></div>
						
						<div id="gOptions" <?php echo ((isset($content['editRule']->id) AND $content['editRule']->global) ? '' : ' style="display: none;"') ?>>	
							<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">		
								<select class="form-control" name="gTrigger">
									<option value="traffic" <?php echo (($content['editRule']->gTrigger == "traffic") ? "selected" : "") ?>>Traffic total (MB)</option>
									<option value="peercount" <?php echo (($content['editRule']->gTrigger == "peercount") ? "selected" : "") ?>>Peer Count</option>								   
								</select>
							</div>
							<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">	
								<input type="text" class="form-control" id="gThreshold" name="gThreshold" value="<?= $content['editRule']->gThreshold ?>" placeholder="Threshold" maxlength="9" size="9">
							</div>
						</div>
				
						<div class="clearfix"></div>
						
						<div class="col-md-5 col-sm-5 col-xs-12 form-group" style="padding:0px 0px 8px 0px;">  
							<button type="submit" class="btn btn-dark">Save</button>
						</div>
					</form>
				</div>
				</div>
				<div class="clearfix"></div>
				</div>			 
			<!-- End Add Rule --> 
 
			<!-- Manage Rules -->
			  <div >
				<div class="x_panel">
				  <div class="x_title">
					<h2>Manage Rules</h2>
					<ul class="nav navbar-right panel_toolbox">
					  <li><a class="close-link"><i class="fa fa-close"></i></a>
					  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
					  <li><a><i class="fa fa-question-circle-o" data-toggle="tooltip" data-html="true" data-placement="left" title="
					  If you click 'Run Rules' all peers are checked for rule violations.<br><br>
					  'Reset Counter' resets the counter how often each rule has been applied.<br><br>
					  'Delete Rules' deletes all rules.<br><br>
					  'Clean logfile' deletes the logfile
					  "></i></a>
					  </li>
					</ul>
					<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<a href="?p=rules&c=run&t=<?= $_SESSION["csfrToken"] ?>" class="btn btn-info" role="button">Run Rules</a>
						<a href="?p=rules&c=resetc&t=<?= $_SESSION["csfrToken"] ?>" class="btn btn-default" role="button">Reset Counter</a>
						<a href="?p=rules&c=delete&t=<?= $_SESSION["csfrToken"] ?>" class="btn btn-default" role="button">Delete Rules</a>
						<a href="?p=rules&c=dellog&t=<?= $_SESSION["csfrToken"] ?>" class="btn btn-default" role="button">Clean Logfile</a>
					</div>
				  <div class="clearfix"></div>
				</div>
			  </div>
			<!-- End Manage Rules --> 
			
			<!-- Cron Job -->
			  <div >
				<div class="x_panel">
				  <div class="x_title">
					<h2>Cron Job</h2>
					<ul class="nav navbar-right panel_toolbox">
					  <li><a class="close-link"><i class="fa fa-close"></i></a>
					  <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
					  <li><a><i class="fa fa-question-circle-o" data-toggle="tooltip" data-html="true" data-placement="left" title="
					  If you use a linux based system you can set up a cron job to run the rules automatically.
					  "></i></a>
					  </li>
					</ul>
					<div class="clearfix"></div>
					</div>
						Use the following command / url to run rules. The token changes after a password change.
						<br /><br />
						<strong>Bash</strong><br />
						<input value="/usr/bin/php <?=getcwd()?>/index.php <?= $content['jobToken'] ?> &>/dev/null" size="50" id="cron" />
						<br /><br /><strong>Url</strong><br />
						<input value="<?=$_SERVER['HTTP_HOST'] . str_replace('index.php', '', $_SERVER['PHP_SELF'])?>?job=<?= $content['jobToken'] ?>" size="50" id="cronurl" />
					</div>
				  <div class="clearfix"></div>
			  </div>
			<!-- End Cron Job -->			  
							</div>

			 <div class="clearfix"></div>
 			
		</div>
	</div>
  </div>
</div>
		<!-- /page content -->

		<!-- footer content -->
		<?php include('views/footer.phtml') ?>
		<!-- /footer content -->

	<script>
	function displayInput(value, svalue, divf) {
		if (value == svalue) {
		   document.getElementById(divf).style.display = "block";
		} else {
		   document.getElementById(divf).style.display = "none";
		}
	}
  
	  function displayInput2(value, svalue, divf, div2) {
		if (value == svalue) {
		   document.getElementById(divf).style.display = "block";
		   document.getElementById(div2).style.display = "none";
		} else if (value == "hosted") {
		   document.getElementById("clientselectiond").style.display = "none";
		   document.getElementById("threshold").style.display = "none";
		} else {
		   document.getElementById(div2).style.display = "block";
		   document.getElementById(divf).style.display = "none";
		}
	}
	</script>
	
	<!-- jQuery -->
	<script src="assets/js/jquery.min.js"></script>
	<!-- Bootstrap -->
	<script src="assets/js/bootstrap.min.js"></script>
	<!-- Datatables -->
	<script src="assets/js/jquery.dataTables.min.js"></script>
	<script src="assets/js/dataTables.bootstrap.min.js"></script>
	<script src="assets/js/dataTables.buttons.min.js"></script>
	<script src="assets/js/buttons.bootstrap.min.js"></script>
	<script src="assets/js/buttons.flash.min.js"></script>
	<script src="assets/js/buttons.html5.min.js"></script>
	<!-- Switchery -->
	<script src="assets/js/switchery.min.js"></script>
	<!-- Custom Theme Scripts -->
	<script src="assets/js/custom.min.js"></script>
	<script src="assets/js/bootstrap-multiselect.js"></script>

	<script type="text/javascript">
	$(document).ready(function() {
		$('#clientselection').multiselect();
		
		var select = document.getElementById('action');	
		var selectT = document.getElementById('trigger');
		displayInput(select.value, "ban", "bantime");		 
		displayInput2(selectT.value, "client", "clientselectiond", "threshold"); 
		

		
		var handleDataTableButtons = function() {
		  if ($("#rules").length) {
			$("#rules").DataTable({
			  dom: '<"dataTables_exportbtn"B>ft',
			  paging:   false,
			  order: [[ 0, "asc" ]],
			  bInfo: false,
			  buttons: [
				{
				  text: 'Export',
				  extend: "csv",
					className: "btn-sm btn-dark",
					exportOptions: {
						columns: [0,3,4,5]
		  		}
				},
			  ],
			  responsive: true
			});
		  }
		};

		TableManageButtons = function() {
		  "use strict";
		  return {
			init: function() {
			  handleDataTableButtons();
			}
		  };
		}();

		TableManageButtons.init();
	});
	
	var hid1 = <?php echo ((isset($content['editRule']->id) AND $content['editRule']->global) ? 'false' : 'true') ?>;
	$("#global").click (function () {

		if(hid1){
			$("#gOptions").show();
			hid1 = false;
		} else {
			$("#gOptions").hide();
			hid1 = true;
		}
	});

	</script>
	<!-- /Datatables -->
  </body>
</html>